<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
session_start();

Class Authentication {

    var $CI;


    public static function login($username, $password) {
        $CI = & get_instance();
        $CI->load->database();
        //$CI->load->session();

        $CI->db->select('*');
        $CI->db->from('users');
        $CI->db->where('username', $username);
        $CI->db->where('password', MD5($password));
        $CI->db->limit(1);
        $query = $CI->db->get();

        if ($query->num_rows() == 1) {
            $rows = $query->result();
            $loggedUser = $rows[0];
            
            // kiểm tra inactive
            if($loggedUser->status == 'inactive')
            {
                return array('Success' => false, 'Message' => 'Tài khoản tạm khóa.');
            }

            $storeInSession = array(
                'sscode' => md5($loggedUser->id) . md5($loggedUser->username),
                'code' => $loggedUser->code,
                'title' => $loggedUser->title,
                'name' => $loggedUser->name,
                'username' => $loggedUser->username,
                'id' => $loggedUser->id,
                'gid' => $loggedUser->gid
            );
            $CI->session->set_userdata('logged_in', $storeInSession);
            //$CI->session->set_userdata('logged_in', md5($loggedUser->id) . md5($loggedUser->username));
            //$_SESSION['logged_in'] = md5($loggedUser->id) . md5($loggedUser->username);
            
            return array('Success' => true, 'LoggedUser' => $loggedUser);
        } else {
            return array('Success' => false, 'Message' => 'Sai username hoặc password');
        }
    }

    public function getLoggedUser() {
        $CI = & get_instance();
        $CI->load->database();
        if(!$CI->session->userdata('logged_in'))
        {
            return false;
        }
        
        $loggedUser = $CI->session->userdata('logged_in');
        
        $CI->db->select('id, username, password, gid, su, title, code, name');
        $CI->db->from('users');
        $CI->db->where('CONCAT(MD5(id), MD5(username)) =', $loggedUser['sscode']);
        $CI->db->limit(1);
        $query = $CI->db->get();
        
        $rows = $query->result();
        $loggedUser = $rows[0];
        return $loggedUser;
    }

    public static function redirectIfLogged() {
        $CI = & get_instance();
        $CI->load->database();

        if ($CI->session->userdata('logged_in')) {
            $loggedUser = Authentication::getLoggedUser();
            switch ($loggedUser->title) {
                case 'SD':
                    redirect('sd/home');
                    break;
                case 'HR':
                    redirect('hr/home');
                    break;
                case 'SC':
                    redirect('sc/home');
                    break;
                case 'RSM':
                    redirect('rsm_kam/home');
                    break;
                case 'KAM':
                    redirect('rsm_kam/home');
                    break;
                case 'SS':
                    redirect('ss_kas/home');
                    break;
                case 'KAS':
                    redirect('ss_kas/home');
                    break;
                default:
                    redirect('user/home');
                    break;
            }
        }
    }
    
    public function redirectIfNotBe($titles)
    {
        $loggedUser = $this->getLoggedUser();
        $allow = false;
        foreach ($titles as $eachTitle) {
            if ($loggedUser->title == $eachTitle)
            {
                $allow = true;
            }
        }
        
        if($allow == false)
        {
            show_404();
        }
        return;
        
    }

}

?>
